package a2;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class EditDialog extends JDialog implements ChangeListener {

	/**
	 * Generate serialVersionID
	 */
	private static final long serialVersionUID = 2214608856596607688L;
	
	private JSpinner xSpinner;
	private JSpinner ySpinner;
	private JSpinner widthSpinner;
	private JSpinner heightSpinner;
	
	private RotatableJFrame frame;
	private JComponent component;


	/**
	 * Constructs a new EditDialog which will allow the user to edit the specified JComponent
	 * @param frame - The parent frame
	 * @param comp	- the JComponent to edit
	 * @param constraints - the constraints on comp
	 */
	public EditDialog(RotatableJFrame frame, JComponent comp, A2Constraints constraints) {
		super(frame);
		
		this.frame = frame;
		this.component = comp;
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(1,2));
		
		// Set up positions panel
		JPanel pPanel = new JPanel();
		pPanel.setBorder(BorderFactory.createTitledBorder(
								BorderFactory.createEtchedBorder(), // use etched border
								Constants.TEXT_POSITION + ": "));	// use constant text + colon
		pPanel.setLayout(new GridLayout(2,1));
		
		// Set up dimensions panel
		JPanel dPanel = new JPanel();
		dPanel.setBorder(BorderFactory.createTitledBorder(
								BorderFactory.createEtchedBorder(), // use etched border
								Constants.TEXT_DIMENSIONS + ": "));	// use constant text + colon
		dPanel.setLayout(new GridLayout(2,1));
		
		// Set up x Position panel
		JPanel xPanel = new JPanel();
		xPanel.add(new JLabel(Constants.TEXT_X_POS + ": "));
		
		xSpinner = new JSpinner();
		xSpinner.setModel(new SpinnerNumberModel
								(constraints.xPos(),// initial value
                                0, 					// min
                                3, 					// max
                                1));				// step)
		xPanel.add(xSpinner);
		
		// Set up y Position panel
		JPanel yPanel = new JPanel();
		yPanel.add(new JLabel(Constants.TEXT_Y_POS + ": "));
		
		ySpinner = new JSpinner();
		ySpinner.setModel(new SpinnerNumberModel
				(constraints.yPos(),// initial value
                0, 					// min
                3, 					// max
                1));				// step)
		yPanel.add(ySpinner);
		
		// Set up width panel
		JPanel wPanel = new JPanel();
		wPanel.add(new JLabel(Constants.TEXT_WIDTH + ": "));
		
		widthSpinner = new JSpinner();
		widthSpinner.setModel(new SpinnerNumberModel
				(constraints.width(),// initial value
                0, 					// min
                3, 					// max
                1));				// step)
		wPanel.add(widthSpinner);
		
		// Set up width panel
		JPanel hPanel = new JPanel();
		hPanel.add(new JLabel(Constants.TEXT_HEIGHT + ": "));
		
		heightSpinner = new JSpinner();
		heightSpinner.setModel(new SpinnerNumberModel
				(constraints.height(),// initial value
                0, 					// min
                3, 					// max
                1));				// step)
		hPanel.add(heightSpinner);
		
		
		// add x and y panels to position panels
		pPanel.add(xPanel);
		pPanel.add(yPanel);
		
		// add w and h panels to dimension panels
		dPanel.add(wPanel);
		dPanel.add(hPanel);
		
		// Add p and d panels to mainPanel
		mainPanel.add(pPanel);
		mainPanel.add(dPanel);
		
		// Add ActionListeners
		xSpinner.addChangeListener(this);
		ySpinner.addChangeListener(this);
		widthSpinner.addChangeListener(this);
		heightSpinner.addChangeListener(this);
		
		// Add mainPanel to dialog, pack, and show
		this.add(mainPanel);
		this.pack();
		this.setVisible(true);
		
		
	}

	/**
	 * Handles the users edits
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		frame.editComponentConstraints( 
				this.component, 						// Component to update
				new A2Constraints(					
					(int) xSpinner.getValue(),			// new X constraint
					(int) ySpinner.getValue(),			// new Y constaint
					(int) widthSpinner.getValue(),		// new width constraint
					(int) heightSpinner.getValue()));	// new height constraint
		
		
	}


}
